using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UnitManager : MonoBehaviour
{

    private GameObject []enemyTempalte;

    private List<GameObject> enemyList;
    public float min_Opt = -4.0f;
    public float max_Opt = 5.0f;

    // Start is called before the first frame update
    void Start()
    {
        enemyTempalte = Resources.LoadAll<GameObject>("Prefabs/Enemy");
    }
    Coroutine runner = null;
    public void StartRun()
    {
        if (transform.childCount == 0)
        {
            runner = StartCoroutine(GeneratePipeLines());
        }
    }

    public void EndRun()
    {
        StopCoroutine(runner);
    }

    IEnumerator GeneratePipeLines()
    {
        while (true)
        {
            CreateEnemy();
            yield return new WaitForSeconds(Random.Range(1.0f, 2f));
        }
    }

    private void CreateEnemy()
    {

        float y = Random.Range(min_Opt, max_Opt);

        GameObject t= Instantiate(enemyTempalte[Random.Range(0,enemyTempalte.Length)], this.transform, false);

        t.transform.localPosition = new Vector3(0, y, transform.localPosition.z);
    }

    public void Init()
    {
        int i = transform.childCount;

        for (int t = 0; t < i; t++)
        {
            Destroy(transform.GetChild(t).gameObject);
        }
    }
}
